﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WebHttpApi.WebHttpApi.Core;

namespace WebHttpApi.WebHttpApi.HttpApi
{
    /// <summary>
    /// TyLora 的摘要说明：安美通集中器 + LoRa水表 指令类
    /// </summary>
    public class TyLora
    {
        public static string str_DeviceId = "";                //集中器地址逆序排列
        public static string str_MonitorPoint = "";            //表监测点号逆序排列
        public static string str_MeterType = "";               //表类型 16进制

        public static string str_MeterId = "";                 //表地址逆序排列
        public static string str_Comm = "";                    //命令串
        public static string str_CommPam = "";                 //参数串

        public static string str_zxjg = "0";                   //执行结果  -  0：未执行 | 1：已执行
        public static string str_xyjx = "";                    //协议解析  -  什么命令（日志）
        public static string str_rzlx = "发送";                //日志类型  -  存储：命令写数据库（日志）
        public static string str_xymc = "";                    //协议名称  -  命令类型名称： 控阀、休眠等（日志）
        public static string str_oper = "WebHttpApi";          //操作员    -  指令、日志

        public static string str_Result = "";

        public static string Lora_Comm(string DeviceId, string MonitorPoint, string MeterType, string MeterId, string Type, string CommPam)
        {
            str_xyjx = "";
            str_xymc = "";

            if (DeviceId != "" && MonitorPoint != "" && MeterId != "" && Type != "")
            {
                try
                {
                    str_DeviceId = DeviceId.Substring(8, 2) +
                                   DeviceId.Substring(6, 2) +
                                   DeviceId.Substring(4, 2) +
                                   DeviceId.Substring(2, 2) +
                                   DeviceId.Substring(0, 2);

                    str_MonitorPoint = (Convert.ToInt64(MonitorPoint)).ToString("X2");
                    str_MonitorPoint = "0000" + str_MonitorPoint;
                    str_MonitorPoint = str_MonitorPoint.Substring(str_MonitorPoint.Length - 4);
                    str_MonitorPoint = str_MonitorPoint.Substring(2, 2) +
                                       str_MonitorPoint.Substring(0, 2);

                    str_MeterId = MeterId.Substring(12, 2) +
                                  MeterId.Substring(10, 2) +
                                  MeterId.Substring(8, 2) +
                                  MeterId.Substring(6, 2) +
                                  MeterId.Substring(4, 2) +
                                  MeterId.Substring(2, 2) +
                                  MeterId.Substring(0, 2);

                    switch (Type)
                    {
                        //控阀指令 
                        //Type：43 | 67
                        //CommPam：55、99、66 | 85、153、102
                        case "67":
                            {
                                if (CommPam == "85")
                                {
                                    str_CommPam = "55";
                                    str_xyjx = "开阀";
                                }
                                else if (CommPam == "153")
                                {
                                    str_CommPam = "99";
                                    str_xyjx = "关阀";
                                }
                                else
                                {
                                    str_Result = "参数错误";
                                    break;
                                }
                                str_xymc = "控阀";

                                if (MeterType == "51" ||
                                    MeterType == "52" ||
                                    MeterType == "53")
                                {

                                    str_MeterType = Convert.ToString(Convert.ToInt32(MeterType), 16);
                                    str_MeterType = "00" + str_MeterType;
                                    str_MeterType = str_MeterType.Substring(str_MeterType.Length - 2);

                                    str_Comm = "68EE" + str_DeviceId + "241400" + str_MonitorPoint + "01" + str_MeterType + str_MeterId + "01000000000000" + str_CommPam + "01";
                                    str_Comm = str_Comm + WebSdkCore.CS(str_Comm);
                                    str_Comm = str_Comm + "16";
                                    str_Comm = str_Comm.ToUpper();

                                    //2020-12-09
                                    //str_Result = Lora_sql(MeterId, Type);
                                    str_Result = Lora_sql(DeviceId, MonitorPoint, MeterType, MeterId, Type);
                                }
                                else
                                {
                                    str_Result = "不支持";
                                }

                                break;
                            }

                        //实时抄表 
                        //Type：53 | 83
                        case "83":
                            {
                                str_xymc = "实时抄表";
                                str_xyjx = "实时抄表";

                                if (MeterType == "51" ||
                                    MeterType == "52" ||
                                    MeterType == "53")
                                {

                                    str_MeterType = Convert.ToString(Convert.ToInt32(MeterType), 16);
                                    str_MeterType = "00" + str_MeterType;
                                    str_MeterType = str_MeterType.Substring(str_MeterType.Length - 2);

                                    str_Comm = "68EE" + str_DeviceId + "18040001" + str_MonitorPoint + "01";
                                    str_Comm = str_Comm + WebSdkCore.CS(str_Comm);
                                    str_Comm = str_Comm + "16";
                                    str_Comm = str_Comm.ToUpper();

                                    //2020-12-09
                                    //str_Result = Lora_sql(MeterId, Type);
                                    str_Result = Lora_sql(DeviceId, MonitorPoint, MeterType, MeterId, Type);
                                }
                                else
                                {
                                    str_Result = "不支持";
                                }

                                break;
                            }
                    }
                }
                catch
                {
                    str_Result = "设备或仪表编号不正确";
                }
            }
            return str_Result;
        }

        //2020-12-09
        //public static string Lora_sql(string MeterId, string Type)
        public static string Lora_sql(string DeviceId, string MonitorPoint, string MeterType, string MeterId, string Type)
        {
            DateTime str_dt = DateTime.Now;

            bool operatorlog = WebSdkCore.insert_update_delete("insert into rp_meter_rz(ybdz,xynr,xyjx,xtsj,rzlx,xymc,czy) values ('" +
                                                                MeterId + "','" +
                                                                str_Comm + "','" +
                                                                str_xyjx + "','" +
                                                                str_dt + "','" +
                                                                str_rzlx + "','" +
                                                                str_xymc + "','" +
                                                                str_oper + "')");
            if (operatorlog == true)
            {
                //2020-12-09
                //只记录阀控指令
                if (Type == "67")
                {
                    string sql = "";
                    string str_zxcs = "0";

                    sql = "select ISNULL(zxcs,'0') as zxcs from rp_meter_cx where sbdz='" + DeviceId + "' and ybdz='" + MeterId + "' and zxjg='0'";

                    System.Data.DataTable Comm_zxcs = WebSdkCore.select(sql);

                    if (Comm_zxcs.Rows.Count > 0)
                    {
                        str_zxcs = Comm_zxcs.Rows[0]["zxcs"].ToString();
                        if (str_zxcs == "")
                        {
                            str_zxcs = "0";
                        }
                    }

                    //执行过3次不在执行，将命令置为已执行
                    bool operator_cx;

                    if (Convert.ToInt16(str_zxcs) > 3)
                    {
                        operator_cx = WebSdkCore.insert_update_delete("update rp_meter_cx set zxjg = '1',zxsj='" +
                                                                       str_dt + "' where ybdz='" +
                                                                       MeterId + "' and sbdz='" +
                                                                       DeviceId + "' and zxjg='0'");

                        if (operator_cx == true)
                        {
                            str_Result = "执行超次数";
                        }
                        else
                        {
                            str_Result = "写指令失败";
                        }
                    }
                    else
                    {
                        str_zxcs = (Convert.ToInt16(str_zxcs) + 1).ToString();

                        operator_cx = WebSdkCore.insert_update_delete("insert into rp_meter_cx(ybdz,ybmc,mllx,mlnr,bcsj,zxjg,czy,sbdz,zxcs) values ('" +
                                                                            MeterId + "','" +
                                                                            MeterType + "','" +
                                                                            Type + "','" +
                                                                            str_Comm + "','" +
                                                                            str_dt + "','" +
                                                                            str_zxjg + "','" +
                                                                            MonitorPoint + "','" +
                                                                            DeviceId + "','" +
                                                                            str_zxcs + "')");

                        if (operator_cx == true)
                        {
                            if (Convert.ToInt16(str_zxcs) > 1)
                            {
                                Global.Send_Jzq(str_Comm);
                            }
                            str_Result = "成功";
                        }
                        else
                        {
                            str_Result = "写指令失败";
                        }
                    }
                }
                else
                {
                    Global.Send_Jzq(str_Comm);
                    str_Result = "成功";
                }
            }
            else
            {
                str_Result = "写日志失败";
            }
            
            return str_Result;
        }
    }
}